var staticParam,queryParam;
$(function(){
    $.fn.raty.defaults.path = '/by-webhtml/assets/image';
    $('.halfShow-true-demo').raty({readOnly: false, score: 5 });
    laydate.skin('yahui');//切换皮肤，请查看skins下面皮肤库
    //日期范围限制
    var start = {
        elem: '#start',
        format: 'YYYY-MM-DD',
        // min: laydate.now(), //设定最小日期为当前日期
        max: '2099-06-16', //最大日期
        istime: true,
        istoday: false,
        choose: function(datas){
            end.min = datas; //开始日选好后，重置结束日的最小日期
            end.start = datas //将结束日的初始值设定为开始日
        }
    };

    var end = {
        elem: '#end',
        format: 'YYYY-MM-DD',
        // min: laydate.now(),
        max: '2099-06-16',
        event: 'click',
        istime: true,
        istoday: false,
        choose: function(datas){
            start.max = datas; //结束日选好后，充值开始日的最大日期
        }
    };
    queryParam = null;
    staticParam = null;
    laydate(start);
    laydate(end);
    $('#start').val(laydate.now(-7, 'YYYY-MM-DD'));
    $('#end').val(laydate.now(0, 'YYYY-MM-DD'));
    getListPage({"curPage":1});
});

function queryPageList() {
    queryParam = null;
    staticParam = null;
    var obj = $(".controlDiv").serializeSearchForm();
    queryParam = $.extend(queryParam,obj);
    goPage(1);
    // getListPage($.extend(staticParam,{"curPage" : 1}));
}
function getListPageByStatus(status,obj,flag) {
    staticParam = null;
    queryParam = null;
    $(".orderTit a").removeClass();
    $(obj).addClass("cur");
    $("#orderPageHead ~ tr").remove();
    var param = $.extend({"status" : status},{"curPage" : 1});
    if(flag == 'evaluation'){
        param = $.extend({"userStars":-1},{"curPage" : 1});
    }else if(flag == 'payStatus'){
        param = $.extend({"payStatus":-1},{"curPage" : 1});
    }
    if(status == '1' || status == '2' || status == '20' || status == '6' || flag == 'evaluation'){
        param = $.extend(param,{"payStatus" : -2});
    }
    if(status == '' && flag != 'evaluation'){
        param = {"curPage" : 1};
    }
    staticParam = param;
    getListPage(param);
}
function getListPage(param){
    param = $.extend(queryParam,staticParam,param);
    $.ajax({
        type : "post",
        url : "htmlWeb/orderManagerList",
        data: $.extend({"userFlag" : 2},param),
        async: false,
        success : function(data){
            var str = "";
            $("#businessOrderPageHead ~ tr").remove();
            $.each(data.rows,function(i,obj){
                obj = trimEmpty(obj);
                var createTime = formatDate(obj.createTime,"yyyy-MM-dd HH:mm:ss");
                var payType = renderPayType(obj.payType);
                var status = renderOrderStatus(obj.status);
                var payStatus = renderOrderPayStatus(obj.payStatus);
                var mobile = obj.user.mobile1 == 'admin' ? "" : obj.user.mobile1;
                if(obj.bizUser && obj.bizUser.bizWork){
                    if(obj.bizUser.bizWork.name == '策划师'){
                        mobile = "******";
                    }else if(!(obj.status == 2 && (obj.payStatus == 1 || obj.payStatus == 2))){
                        mobile = "******";
                    }
                }
                str += '<tr><td><label><input type="checkbox" name="subBox" class="selectDiv" /></label></td>' +
                    '<td><p>' + obj.id + '</p></td>' +
                    '<td><p>' + obj.user.cnName + '</p></td>' +
                    '<td><p>' + mobile + '</p></td>' +
                    '<td><p>' + payType + '</p></td>' +
                    '<td><p>' + obj.price + '</p></td>' +
                    '<td><p>' + obj.deposit + '</p></td>' +
                    '<td><p>' + status + '</p></td>' +
                    '<td><p>' + payStatus + '</p></td>' +
                    '<td><p>' + createTime + '</p></td>' +
                    '<td><span>' + obj.descn + '</span></td>' +
                    '<td><span class="userSpan">' +
                    '<a href="javascript:void(0);" onclick="showOrderInfo(' + obj.id + ')">订单详情</a>' ;


                if(obj.payStatus != -1 && obj.status == 1){
                    str += '<a href="javascript:void(0);" onclick="confirmOrder(' + obj.id + ',2)">接单确认</a>' ;
                }else if(obj.status == 2){
                    str += '<a href="javascript:void(0);" class="finished" onclick="openPopupOrder(' + obj.id + ')">订单完成</a>' ;
                }else if(obj.status == 4){
                    str += '<a href="javascript:void(0);" onclick="confirmOrder(' + obj.id + ',7)">同意退款</a>' ;
                    str += '<a href="javascript:void(0);" onclick="confirmOrder(' + obj.id + ',8)">拒绝退款</a>' ;
                }
                str += '</span></td></tr>';
            });

            $("#businessOrderPageHead").after(str);
            createPage(data);
        },
        error : function(data){
            console.log(data);
        }
    });
}

/**
 * 查看详情
 * @param id
 */
function showOrderInfo(id){
    $(".mainDiv").load("systemOrderDetailsHtml",'',function(){
        $("#orderId").val(id);
        $("#parentFlag").val("business");
        $.getScript("../../by-webhtml/assets/js/system/system_orderDetails.js");
    });
}
/**
 *
 * @param id
 * @param status  2 商家确认  5 商家订单完成
 */
function confirmOrder(id,status){
    $.post("htmlWeb/updateOrderStatus",{"order_id":id,"status":status},function(data, textStatus, jqXHR){
        // alert("商家已接单");
        navPills("systemBusinessHtml");
    });
}
var orderId ;
//商家完成订单确认弹出框
function openPopupOrder(id) {
    orderId = id;
    $(".deskDiv").css({
        display:"block",height:$(document).height()
    });
    $(".orderOver").css({
        top:($(window).height()-$(".orderOver").height())/2+$(window).scrollTop()+"px",
        display:"block"
    });
}

//完成订单
function overOrder() {
    closePopup();
    $.post("htmlWeb/updateOrderStatus",{"order_id":orderId,"status":5},function(data, textStatus, jqXHR){
        // alert("商家已接单");
        navPills("systemBusinessHtml");
    });
}
//关闭弹出框
function closePopup(){
    $(".deskDiv").fadeOut();
    $(".orderOver").fadeOut();
}